package com.dao;

import com.entity.UserFile;
import com.util.DBpool.DBconnection;

import java.util.ArrayList;

/**
 * Created by wlc on 2018/9/12.
 */
public class UserFileDao {
    /**
     * 保存用户文件
     */
    public static void saveUserFile(UserFile userFile){
        DBconnection connection = new DBconnection();
        connection.setSheet_name("userFiles");
        connection.saveDataToDB(userFile.userFileToList());
    }

    /**
     * 获取文件数据库中的所有数据,
     * @return
     */
    public static ArrayList<UserFile> getAllFilesList(){
        ArrayList<UserFile> filesList = new ArrayList<>();
        ArrayList<ArrayList<String>> temp = new ArrayList<>();

        DBconnection connection = new DBconnection();
        connection.setSheet_name("userFiles");
        temp = connection.getDataFromDB();
        for (ArrayList<String> file : temp){
            UserFile tempFile = new UserFile();
            tempFile.listToUserFile(file);
            filesList.add(tempFile);
        }

        return filesList;
    }

    /**
     * 删除指定ID文件
     * @param fileId
     * @param isForce 是否强制删除，无视状态
     */
    public static void deleteUserFile(String fileId,boolean isForce){
        DBconnection connection = new DBconnection();
        connection.setSheet_name("userFiles");
        if (!isForce){
            String fileStatus = connection.getDataFromRow(fileId,"status");
            switch (fileStatus){
                case "已发表":
                    connection.changeDataToDB("status","已发表-回收站","id",fileId);
                    break;
                case "草稿箱":
                    connection.changeDataToDB("status","草稿箱-回收站","id",fileId);
                    break;
                case "已发表-回收站":
                    connection.deleteDataInDB("id",fileId);
                    break;
                case "草稿箱-回收站":
                    connection.deleteDataInDB("id",fileId);
                    break;
            }
        }else {
            connection.deleteDataInDB("id",fileId);
        }

    }

    /**
     * 还原指定ID文件
     * @param fileId
     */
    public static void reductionUserFile(String fileId){
        DBconnection connection = new DBconnection();
        connection.setSheet_name("userFiles");
        String fileStatus = connection.getDataFromRow(fileId,"status");
        switch (fileStatus){
            case "已发表-回收站":
                connection.changeDataToDB("status","已发表","id",fileId);
                break;
            case "草稿箱-回收站":
                connection.changeDataToDB("status","草稿箱","id",fileId);
                break;
        }
    }

    /**
     * 获取指定ID文件
     * @param fileId
     * @return
     */
    public static UserFile getFileById(String fileId){
        DBconnection connection = new DBconnection();
        connection.setSheet_name("userFiles");
        ArrayList<String> fileList = connection.getRowDataFromDB(fileId);
        UserFile userFile = new UserFile();
        userFile.listToUserFile(fileList);
        return userFile;
    }
}
